/**
 *
 */
package com.nest.WorkNest.act;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lidroid.xutils.exception.HttpException;
import com.nest.WorkNest.R;
import com.nest.WorkNest.adapter.StatusAdapter;
import com.nest.WorkNest.api.XutilsPost;
import com.nest.WorkNest.tools.Tool;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 */
public class StatusRecordAct extends BaseActivity {

    @Bind(R.id.record_proname)
    TextView recordProname;
    @Bind(R.id.record_orderid)
    TextView recordOrderid;
    @Bind(R.id.record_proid)
    TextView recordProid;
    @Bind(R.id.tv_record_proid)
    TextView tvRecordProid;
    @Bind(R.id.record_img)
    ImageView recordImg;
    private ListView logistics;
    private StatusAdapter adapter;
    private ArrayList<HashMap<String, String>> dataList = new ArrayList<HashMap<String, String>>();
    private Context context = StatusRecordAct.this;
    private String bulkOrderId, productId, productName, imgUrl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_status_record);
        ButterKnife.bind(this);
        initBar();
        actionbar_side_tab_name.setVisibility(View.VISIBLE);
        actionbar_side_tab_name.setText("状态记录");
        ll_iv_actionbar_tab_side_left.setVisibility(View.VISIBLE);
        tabBack();
        initView();
        loadStatus();
//      initData();
        adapter = new StatusAdapter(this, dataList);
        logistics.setAdapter(adapter);
    }

    public void loadStatus() {
        bulkOrderId = getIntent().getStringExtra("bulkOrderId");
        if (!TextUtils.isEmpty(getIntent().getStringExtra("imgUrl"))) {
            imgUrl = getIntent().getStringExtra("imgUrl");
            Glide.with(context)
                    .load(imgUrl)
                    .centerCrop()
                    .placeholder(R.drawable.empty_photo)
                    .crossFade()
                    .into(recordImg);
        }
        if (TextUtils.isEmpty(getIntent().getStringExtra("productId"))) {
            recordProid.setVisibility(View.GONE);
            tvRecordProid.setVisibility(View.GONE);
        } else {
            productId = getIntent().getStringExtra("productId");
        }
        productName = getIntent().getStringExtra("productName");
        recordOrderid.setText(bulkOrderId);
        recordProid.setText(productId);
        recordProname.setText("商品名称: " + productName);
        XutilsPost xutilsPost = new XutilsPost();
        XutilsPost.IOAuthCallBack ioAuthCallBack = new XutilsPost.IOAuthCallBack() {
            @Override
            public void getIOAuthCallBack(String result) {
                if (Tool.getString(result, "resultCode").equals("0")) {
                    JSONArray array = Tool.getJsonArray(result, "datalist");
                    dataList.clear();
                    dataList = Tool.getListMapByJsonArray(array);
                    if (dataList.size() != 0) {

                        for (int i = 0; i < dataList.size() - 1; i++) {
                            dataList.get(i).put("state", "uncheck");
                        }
                        dataList.get(dataList.size() - 1).put("state", "check");
                    }
                    adapter.update(dataList);
                } else if (Tool.getString(result, "resultCode").equals("9001")) {
                    showToast("token已过期，请重新登录");
                    Intent intent = new Intent(context, LoginAct.class);
                    startActivity(intent);
                    finish();
                }

            }

            @Override
            public void getDealException(HttpException httpException, String result) {

            }

            @Override
            public void getLoadingCallBack(long total, long current, boolean isUploading) {

            }
        };
        xutilsPost.doPostStatus(context, bulkOrderId, ioAuthCallBack);
    }

    private void initView() {
        logistics = (ListView) findViewById(R.id.logistics_list);
    }

//    protected void onResume() {
//        loadStatus();
//        super.onResume();
//    }

}
